/*
 * @Author       : 邱狮杰
 * @Date         : 2021-09-03 15:09:58
 * @LastEditTime: 2021-09-05 15:37:23
 * @FilePath: /new-hope/src/hooks/http.ts
 * @Description  :
 */

import { SynchronizationAwaitError } from "synchronizationawaiterror";
import { AxiosRequestConfig, AxiosResponse } from "axios";
import { http } from "/@/config/index";
export function useHttp() {
  async function send<T>(
    config: AxiosRequestConfig,
    successCb?: (val: AxiosResponse<T>) => void,
    failCb?: () => void
  ): Promise<void | AxiosResponse<T>> {
    const [err, res] = await SynchronizationAwaitError(http(config));
    if (err) {
      failCb?.();
      return;
    }
    successCb?.(res as AxiosResponse<T>);
    return res as Promise<AxiosResponse<T>>;
  }
  return [send];
}
